package models
{
	import mx.controls.Alert;
	
	public class Diagram extends Resource
	{
		private var _cdataParsed:Boolean = false;
		
		public function Diagram(projectId:int = 0, type:String = "", name:String = "") {
			super(new XML('<diagram />'));
			this.type 		= type;
			this.name 		= name;
			this.projectId 	= projectId;
		}
		
		public function get oid() : int {
			return _node.@oid;
		}
		public function set oid(oid:int) : void {
			_node.@oid = oid;
		}
		
		public function get projectId() : int {
			return _node.@projectId;
		}
		public function set projectId(projectId:int) : void {
			_node.@projectId = projectId;
		}
		
		public function get type() : String {
			return _node.@type;
		}
		public function set type(type:String) : void {
			_node.@type = type;
		}
		
		public function get name() : String {
			return _node.@name;
		}
		public function set name(name:String) : void {
			_node.@name = name;
		}
		
		public function get layout() : XML {
			if(!_cdataParsed) {
				var str:String = _node.layout.text();
				str = str.replace(new RegExp('/&lt;/', 'g'), '<');
				str = str.replace(new RegExp('/&gt;/', 'g'), '>');
				delete _node.layout.*;
				_node.layout.appendChild(new XML(str));
				_cdataParsed = true;  
			}
			return _node.layout.*[0];
		}
		public function set layout(layout:XML) : void {
			delete _node.layout.*;
			 _node.layout.appendChild(layout);
		}
		
		public override function toXML(deep:Boolean = true) : XML {
			if(_node.layout.length()) {
				var layout:XML = this.layout;
				delete _node.layout.*;
				_node.layout.appendChild(new XML('<![CDATA[' + layout.toXMLString() + ']]>'));
			}
			return super.toXML(deep);
		}
	}
}